//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.StrockRackQueryController');
	com.yonyou.ncmm_pac_pda.StrockRackQueryController = function() {
		com.yonyou.ncmm_pac_pda.StrockRackQueryController.initializeBase(this);
		this.initialize();
	}
	var pk_stordoc = "";
	var stordoc_code = "";
	var stordoc_name = "";
	var pk_material = "";
	var material_code = "";
	var DOCTYPE_NULL = "MAPP_ONHANDNUM_CSCODENULL";

	function com$yonyou$ncmm_pac_pda$StrockRackQueryController$initialize() {

	}

	function com$yonyou$ncmm_pac_pda$StrockRackQueryController$evaljs(js) {
		eval(js)
	}

	function onEventClick_setDefaultReposity(sender, args) {
		openRefPage("PDASTORDOC", "stordoc_name", "pk_stordoc", "pk_stordoc");

	}

	// 打开参照页面
	function openRefPage(ncbillcode, ctxnamekey, ctxpkkey, pkfiel) {
		var jsonparam = {};
		getRefParam(ncbillcode, function(data) {
			jsonparam = data;
		});
		if(jsonparam == null) {
			return;
		}
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: jsonparam,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxnamekey, refdata["name"]);
				$ctx.put(ctxpkkey, refdata[pkfiel]);
				pk_stordoc = refdata[pkfiel];
				stordoc_name = refdata["name"];
				$ctx.dataBind();
				$cache.write("default" + ctxnamekey, $jsonToString(refdata));

				//设置了默认库存组织后，自动下载默认库存组织下的仓库
				//where pk_org='库存组织编码'
				if(ctxnamekey == "stockorg") {
					var condition = {
						"pk_org": "='" + refdata["code"] + "'"
					};
					processDocDataFromNC(StordocVO, condition);
				}
			}
		});
	}

	//获取参照vo的vo信息
	//reftype： 外系统调用标识
	function getRefParam(reftype, callback) {
		var condition = {
			"ncbillcode": reftype
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				$alert("【" + reftitle + "】没有可参照的数据");
				if(callback != null && callback != undefined && typeof(callback) == 'function') {
					callback(null);
				}
				return;
			}
			var reftablename = (jsondata.bdlistdata)[0]["apptablename"]; //本地表名
			var jsonparam = {
				reftype: reftype, //外系统调用标识
				reftitle: reftitle, //元数据显示名称
				reftablename: reftablename //本地表名
			};
			if(callback != null && callback != undefined && typeof(callback) == 'function') {
				callback(jsonparam);
			}
		});
	}

	/**
	 * 空货位查询
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function onEventClick_queryStrockRackQuery(sender, args) {

		if(!pk_stordoc) {
			$toast("请先选择仓库!");
			return;
		}
		var pk_rack = $ctx.get("pk_rack");
		if(NCMMPubUtil.isEmpty(pk_rack)) {
			$toast("请先选择货位!");
			return;
		}

		//$alert("vcode:" + vcode + ",pk_stordoc:" + pk_stordoc);
		//查询存量
		$js.showLoadingBar({
			msg: '正在查询数据，请等待！'
		});
		var condition = {};
		condition['storcode_h'] = "=\'" + pk_stordoc + "\'";
		condition['pk_rack'] = "=\'" + pk_rack + "\'";
		//测试连接
		NCMMDBUtil.testNCConnection(function(data1) {
			if(data1 == false || data1 == "false") {
				$js.hideLoadingBar();
				$toast("查询失败,请检查网络设置！");
				return;
			} else {
				queryByStoreRack(condition, function(data) {
					if(data != null) {
						$js.hideLoadingBar();
						//绑定列表
						var arr = new Array();
						var billinfos = [];
						var showData = [];
						billinfos = data["billinfos"];
						if(NCMMPubUtil.isNotEmpty(billinfos) && billinfos.length > 0) {
							for(var val = 0; val < billinfos.length; val++) {
								//alert("billinfos[" + val + "]:" + JSON.stringify(billinfos[val]));
								var billinfo = billinfos[val].billinfo;
								var head = billinfo.head;
								var items = billinfo.items;
								arr = new Array();
								arr.push("物料：" + head.vinvcode + " [" + head.vinvname + "]\n");
								arr.push("规格 ：" + (!head.vinvspec ? "" : head.vinvspec) + "型号：" + (!head.vinvtype ? "" : head.vinvtype) + "\n");
								arr.push("批次号：" + (!head.vbatchcode ? "" : (head.vbatchcode + " [" + head.tbatchtime + "]")) + "\n");
								arr.push("主单位：" + head.cmeasname + "  单位：" + head.measname + "\n");
								arr.push("结存主数量：" + items[0].item.nquantity + "   结存数量：" + head.nonhandastnum + "\n");
								arr.push("----------------------------------------------\n");
								showData.push({
									"result": arr.join("")
								});
							}
							showData = NCMMPubUtil.htmlspecialjsonarray2UI(showData);
							$ctx.put("listdata", eval(showData));
							$ctx.dataBind();
							$js.hideLoadingBar();
							$toast("查询到【" + billinfos.length + "】条数据。");
						} else {
							$js.hideLoadingBar();
							$toast("没有查询到存量数据！");
							$ctx.put("listdata", {});
							$ctx.dataBind();
						}
					} else {
						$js.hideLoadingBar();
						$toast("没有查询到存量数据！");
						$ctx.put("listdata", {});
						$ctx.dataBind();
					}

				});
			}
		});
	}

	function queryByStoreRack(condition, callback) {
		//拼装HTTP请求参数
		var httpURL = NCMMHttpUtil.getQuestURL(NCMMHttpUtil.BILL);
		var aggvo = "aggvo";
		var httpparam = NCMMHttpUtil.getHTTPRequestParam("MAPP_ONHANDNUM_INV", aggvo, condition);
		//发送HTTP请求	
		NCMMHttpUtil.sendHTTPRequest(httpURL, httpparam, function(data, issuccess) {
			if(issuccess > 0) {
				if(issuccess == 1) { //成功
					if(callback != null && typeof(callback) == 'function') {
						//	$alert("data=" + $jsonToString(data));
						callback(data);
					}
				} else {
					//afterError("下载", "NC端没有返回单据记录"); //成功但记录为空
					if(callback != null && typeof(callback) == 'function') {
						callback(data);
						//callback(null);
					};
				}
			} else { //失败
				afterError("下载", data);
				if(callback != null && typeof(callback) == 'function') {
					callback(null);
				};
			}
		});
	};

	function com$yonyou$ncmm_pac_pda$StrockRackQueryController$button0_onclick(sender, args) {
		$view.close();
	}

	function onEvent_onLoad(sender, args) {
		var stordoc = $stringToJSON($cache.read("defaultstordoc"));
		if(!!stordoc) {
			stordoc = $stringToJSON(stordoc);
			var stordocvos = NCMMDBUtil.getVOsByPK(StordocVO, "pk_stordoc", stordoc["pk_stordoc"]); //typeof(stordocvo) == "object"
			var stordocvo = null;
			if(stordocvos) {
				stordocvo = stordocvos[0];
			}
			if(stordocvo) {
				stordoc_name = NCMMPubUtil.htmlspecialchars2UI(stordocvo["name"]);
				pk_stordoc = stordoc.pk_stordoc;
				stordoc_code = NCMMPubUtil.htmlspecialchars2UI(stordocvo["code"]);
				$ctx.put("stordoc_name", stordoc_name);
				$ctx.dataBind();
			}
		}

	}

	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("btn_menu");
	}

	function rackRef(sender, args) {
		if(!pk_stordoc) {
			$toast("请先选择仓库!");
			return;
		}
		var wheresql = " where pk_stordoc='" + pk_stordoc + "'";
		NCMMPubUtil.openRefPage("PDARACK", "rack_name", "pk_rack", "pk_rack", wheresql);
	}

	function rackScan(sender, args) {
		if(!pk_stordoc) {
			$toast("请先选择仓库!");
			return;
		}
		$scanner.open({
			bindfield: "rack",
			callback: "rackScanBack()"
		});
	}

	function rackScanBack() {
		var rack = $ctx.getString("rack");
		var wheresql = " where code='" + rack + "'";
		if(!NCMMPubUtil.isEmpty(pk_stordoc)) {
			wheresql = wheresql + " and pk_stordoc='" + pk_stordoc + "'";
		}
		getRackInfoByWhereSql(wheresql, rack, "pk_rack", "rack_name");
	}

	function getRackInfoByWhereSql(wheresql, rack, pkField, nameField) {
		if(NCMMPubUtil.isTableExist(RackVO) == false) {
			$alert("没有扫描货位【" + rack + "】记录，请更新货位数据，或检查当前扫描货位是否属于默认仓库！");
			$ctx.put(nameField, "");
			$ctx.put(pkField, "");
			$ctx.dataBind();
			return;
		}
		NCMMPubUtil.queryVO(RackVO, wheresql, function(data) {
			//2 打开扫描记录编辑界面
			if(NCMMPubUtil.isEmpty(data)) {
				$alert("没有扫描货位【" + rack + "】记录，请更新货位数据，或检查当前扫描货位是否属于默认仓库！");
				$ctx.put(nameField, "");
				$ctx.put(pkField, "");
				$ctx.dataBind();
				return;
			}
			var vos = eval(data);
			$ctx.put(nameField, vos[0].name);
			$ctx.put(pkField, vos[0].pk_rack);
			$ctx.dataBind();
		});
	}

	function rackEdit(sender, args) {
		if(!pk_stordoc) {
			$toast("请先选择仓库!");
			return;
		}
		var rack = $ctx.getString("rack_name");
		if(NCMMPubUtil.isEmpty(rack)) {
			$ctx.put("rack_name", "");
			$ctx.put("pk_rack", "");
			$ctx.dataBind();
			return;
		}
		var wheresql = " where (code='" + rack + "' or name ='" + rack + "')";
		if(!NCMMPubUtil.isEmpty(pk_stordoc)) {
			wheresql = wheresql + " and pk_stordoc='" + pk_stordoc + "'";
		}
		getRackInfoByWhereSql(wheresql, rack, "pk_rack", "rack_name");
	}
	com.yonyou.ncmm_pac_pda.StrockRackQueryController.prototype = {
		button0_onclick: com$yonyou$ncmm_pac_pda$StrockRackQueryController$button0_onclick,
		initialize: com$yonyou$ncmm_pac_pda$StrockRackQueryController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$StrockRackQueryController$evaljs
	};
	com.yonyou.ncmm_pac_pda.StrockRackQueryController.registerClass('com.yonyou.ncmm_pac_pda.StrockRackQueryController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}